<?php
namespace App\Admin\Controllers;
use App\Models\Class_section_group;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class Linked_search_squadController extends Controller {
	/**
	 * [获取联动数据]
	 * @param int stage 	班段 (全部:传0)
	 * @param int group 	分组
	 * @param int type 		班型
	 * @param int year 		开班年份
	 * @param int campuses 	校区
	 * @return array
	 */
	public function getData(Request $request) {
		try {
			$stage = $request->get("stage");
			$group = $request->get("group");
			$type = $request->get("type");
			$year = $request->get("year");
			$campus = $request->get("campuses");
			//根据班段获取分组
			$where_stage = [['type',"=","2"]];
			if($stage){
				$where_stage[] = ['pid',"=",$stage];
			}
			// 查出班号
			$db = DB::table('squads');
			if($group){
				$db = $db->where('group',"=",$group);
			}
			if($stage){
				$db = $db->where('section',"=",$stage);
			}
			if($year){
				$db = $db->where('batch',"like",$year."-%");
			}
			if($campus){
				$db = $db->where('campus',"=",$campus);
			}
			if($type){
				$db = $db->where('type',"=",$type);
			}
			$data = [
				"stage" => Class_section_group::regulate_options(),
				"type" => DB::table("class_types")->select(['id','name'])->get(),
				"group" => Class_section_group::where($where_stage)->get(['id','name']),
				"campuses" => DB::table("campuses")->select(['id','name'])->get(),
				"classes" => $db->get(["id","name"]),
			];
			return ['code'=>1,'msg'=>'获取数据成功','data'=>$data];
		} catch (\Exception $e) {
			return ['code'=>0,'msg'=>'获取数据失败，请稍后...'.$e->getMessage(),'data'=>""];
		}
	}
}
